package activity.live.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import com.hichip.data.HiDeviceInfo;
import com.hichip.sdk.HiChipP2P;
import activity.live.LiveViewActivity;
import activity.live.LiveViewModel;
import base.HiFragment;
import bean.MyCamera;
import butterknife.BindView;
import butterknife.ButterKnife;
import common.HiDataValue;
import common.MyLiveViewGLMonitor;
import custom.SwitchButton;
import utils.HiToast;

public class SecondTabFrament extends HiFragment implements ICameraIOSessionCallback, View.OnClickListener, CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {

    private MyCamera mMyCamera;
    private int mHeight;
    HiChipDefines.HI_P2P_S_DISPLAY display_param = null;
    @BindView(R.id.ll_jxfz) public LinearLayout ll_jxfz;
    @BindView(R.id.ll_jjjj) public LinearLayout ll_jjjj;
    @BindView(R.id.ll_yzw) public LinearLayout ll_yzw;
    @BindView(R.id.ll_ylkz) public LinearLayout ll_ylkz;
    @BindView(R.id.ll_txtj) public LinearLayout ll_txtj;

    private HiChipDefines.HI_P2P_S_AUDIO_ATTR audio_attr;
    private int maxInputValue = 100;
    private int maxOutputValue = 100;
    private SeekBar seekbar_input;
    private SeekBar seekbar_output;

    public PopupWindow mPopupWindow;

    private boolean mIsTXTJ = false;//是否是图像调节  用来区分 点击镜像翻转和图像调节发的命令
    private TextView tv_brightness;
    private TextView tv_contrast;
    private TextView tv_saturation;

    @BindView(R.id.ll_added_item_one) public LinearLayout ll_added_item_one;
    @BindView(R.id.iv_added_item_one) ImageView iv_added_item_one;
    @BindView(R.id.tv_added_item_one) TextView tv_added_item_one;

    @BindView(R.id.ll_added_item_two) public LinearLayout ll_added_item_two;
    @BindView(R.id.iv_added_item_two) ImageView iv_added_item_two;
    @BindView(R.id.tv_added_item_two) TextView tv_added_item_two;
    public int lightModel = 0;// 0 non ,1 HI_P2P_WHITE_LIGHT_GET_EXT ,2
    // HI_P2P_WHITE_LIGHT_GET 3.纯白光灯
    public boolean mIsHaveTrack = false;
    private HiChipDefines.HI_P2P_WHITE_LIGHT_INFO_EXT light_info_ext;
    private HiChipDefines.HI_P2P_WHITE_LIGHT_INFO light_info;
    private HiChipDefines.ABSOLUTE_LIGHT_TYPE abs_light;
    protected HiChipDefines.HI_P2P_SMART_MOTO trackParam;
    private LiveViewActivity mAct;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_live_second_tab, null);
        ButterKnife.bind(this, view);
        initViewAndData();
        setListeners();
        return view;
    }

    private void setListeners() {
        ll_jxfz.setOnClickListener(this);
        ll_jjjj.setOnClickListener(this);
        ll_yzw.setOnClickListener(this);
        ll_ylkz.setOnClickListener(this);
        ll_txtj.setOnClickListener(this);
        ll_added_item_one.setOnClickListener(this);
    }

    private void initViewAndData() {
        mAct = (LiveViewActivity) getActivity();
        //白光灯
        getLightModel();
        if (lightModel != 0) {
            iv_added_item_one.setImageResource(R.drawable.selector_ysms);
            tv_added_item_one.setText(getString(R.string.night_model));
        }
        //智能追踪
        mIsHaveTrack = mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_GET_SMART_MOTO);
        if (mIsHaveTrack) {
            if (lightModel != 0) {
                iv_added_item_two.setImageResource(R.drawable.selector_znzz);
                tv_added_item_two.setText(getString(R.string.tracking));
            } else {
                iv_added_item_one.setImageResource(R.drawable.selector_znzz);
                tv_added_item_one.setText(getString(R.string.tracking));
            }
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_SMART_MOTO, new byte[0]);
        }
    }

    private void getLightModel() {
        boolean b = mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_GET_ABSOLUTE_LIGHT_TYPE);
        if (b) {
            lightModel = 3;
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_ABSOLUTE_LIGHT_TYPE, null);
            return;
        }
        b = mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_WHITE_LIGHT_GET_EXT);
        if (b) {
            lightModel = 1;
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_WHITE_LIGHT_GET_EXT, null);
            return;
        }
        b = mMyCamera.getCommandFunction(HiChipDefines.HI_P2P_WHITE_LIGHT_GET);
        if (b) {
            lightModel = 2;
            mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_WHITE_LIGHT_GET, null);
        }
    }


    public void initHeight(int height) {
        this.mHeight = height;
    }

    public void setmMyCamera(MyCamera camera) {
        this.mMyCamera = camera;
        mMyCamera.registerIOSessionListener(this);
    }

    public void RegisterMyCamera() {
        mMyCamera.registerIOSessionListener(this);
    }

    public void unRegisterMyCamera() {
        mMyCamera.unregisterIOSessionListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mMyCamera != null) {
            mMyCamera.unregisterIOSessionListener(this);
        }
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
        mPopupWindow = null;
    }

    @Override
    public void receiveSessionState(HiCamera arg0, int arg1) {
        if (mMyCamera != arg0) return;
        Message msg = mHandler.obtainMessage();
        msg.what = HiDataValue.HANDLE_MESSAGE_SESSION_STATE;
        msg.arg1 = arg1;
        msg.obj = arg0;
        mHandler.sendMessage(msg);
    }

    @Override
    public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
        if (arg0 != mMyCamera) return;
        Bundle bundle = new Bundle();
        bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
        Message msg = mHandler.obtainMessage();
        msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        msg.obj = arg0;
        msg.arg1 = arg1;
        msg.arg2 = arg3;
        msg.setData(bundle);
        mHandler.sendMessage(msg);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_jxfz:
                //if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                mIsTXTJ = false;
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_DISPLAY_PARAM, null);
                break;
            case R.id.ll_jjjj://聚焦加减
                if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                handZoomFocus();
                break;
            case R.id.ll_yzw://预置位
                if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                handPreset();
                break;
            case R.id.ll_ylkz://音量控制
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_AUDIO_ATTR, null);
                break;
            case R.id.ll_added_item_one:
                if (lightModel != 0) {
                    if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                    showPupLight();
                    return;
                }
                if (lightModel == 0 && mIsHaveTrack) {
                    handTrack();
                }
                break;
            case R.id.ll_txtj://图像调节
                mIsTXTJ = true;
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_DISPLAY_PARAM, null);
                break;
        }
    }

    private void handTrack() {
        int isChecked = 0;
        if (ll_added_item_one.isSelected()) {
            isChecked = 0;
            ll_added_item_one.setSelected(false);
            mAct.iv_ydzz_land.setSelected(false);
            HiToast.showToast(getContext(), getString(R.string.tracking_close));
        } else {
            isChecked = 1;
            ll_added_item_one.setSelected(true);
            mAct.iv_ydzz_land.setSelected(true);
            HiToast.showToast(getContext(), getString(R.string.tracking_open));
        }
        mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_SMART_MOTO, trackParam.parseContent(isChecked));
    }

    private void showPupLight() {
        View customView = View.inflate(getContext(), R.layout.pup_light, null);
        mPopupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setAnimationStyle(R.style.AnimToButtom);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mPopupWindow.showAtLocation(customView, Gravity.CENTER, 0, dm.heightPixels);
        ImageView iv_close = customView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        RadioButton[] lightRadioBtnsRES = new RadioButton[3];
        RadioButton[] lightRadioBtnsNOR = new RadioButton[2];

        RadioGroup live_view_ext_layout = (RadioGroup) customView.findViewById(R.id.live_view_ext_layout);
        RadioGroup live_view_nor_layout = (RadioGroup) customView.findViewById(R.id.live_view_nor_layout);
        RadioGroup live_view_abs_light_layout = (RadioGroup) customView.findViewById(R.id.live_view_abs_light_layout);
        lightRadioBtnsRES[0] = (RadioButton) customView.findViewById(R.id.live_view_ext_btn_normal);
        lightRadioBtnsRES[1] = (RadioButton) customView.findViewById(R.id.live_view_ext_btn_color);
        lightRadioBtnsRES[2] = (RadioButton) customView.findViewById(R.id.live_view_ext_btn_auto);
        lightRadioBtnsNOR[0] = (RadioButton) customView.findViewById(R.id.live_view_nor_btn_open);
        lightRadioBtnsNOR[1] = (RadioButton) customView.findViewById(R.id.live_view_nor_btn_close);

        if (lightModel == 2) {
            live_view_ext_layout.setVisibility(View.GONE);
            live_view_abs_light_layout.setVisibility(View.GONE);
            live_view_nor_layout.setVisibility(View.VISIBLE);
            if (light_info != null && light_info.u32State < 2) {
                lightRadioBtnsNOR[light_info.u32State].setChecked(true);
            }
        } else if (lightModel == 1) {
            live_view_ext_layout.setVisibility(View.VISIBLE);
            live_view_nor_layout.setVisibility(View.GONE);
            live_view_abs_light_layout.setVisibility(View.GONE);
            if (light_info_ext != null && light_info_ext.u32State < 3) {
                lightRadioBtnsRES[light_info_ext.u32State].setChecked(true);
            }
        } else if (lightModel == 3) {
            live_view_abs_light_layout.setVisibility(View.VISIBLE);
            live_view_ext_layout.setVisibility(View.GONE);
            live_view_nor_layout.setVisibility(View.GONE);
            if (abs_light != null && abs_light.s32State < 3) {
                RadioButton rBtn = (RadioButton) live_view_abs_light_layout.getChildAt(abs_light.s32State);
                rBtn.setChecked(true);
            }
        }

        live_view_ext_layout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup rg, int id) {
                if (light_info_ext == null) return;
                switch (id) {
                    case R.id.live_view_ext_btn_normal:
                        light_info_ext.u32State = 0;
                        break;
                    case R.id.live_view_ext_btn_color:
                        light_info_ext.u32State = 1;
                        break;
                    case R.id.live_view_ext_btn_auto:
                        light_info_ext.u32State = 2;
                        break;
                }
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_WHITE_LIGHT_SET_EXT, HiChipDefines.HI_P2P_WHITE_LIGHT_INFO_EXT.parseContent(light_info_ext.u32Chn, light_info_ext.u32State));
            }
        });

        live_view_nor_layout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup rg, int id) {
                if (light_info == null) return;
                switch (id) {
                    case R.id.live_view_nor_btn_open:
                        light_info.u32State = 0;
                        break;
                    case R.id.live_view_nor_btn_close:
                        light_info.u32State = 1;
                        break;
                }
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_WHITE_LIGHT_SET, HiChipDefines.HI_P2P_WHITE_LIGHT_INFO.parseContent(light_info.u32Chn, light_info.u32State));

            }
        });

        live_view_abs_light_layout.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (abs_light == null) return;
                switch (checkedId) {
                    case R.id.abs_light_auto:
                        abs_light.s32State = 0;
                        break;
                    case R.id.abs_light_open:
                        abs_light.s32State = 1;
                        break;
                    case R.id.abs_light_close:
                        abs_light.s32State = 2;
                        break;
                }
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_ABSOLUTE_LIGHT_TYPE, HiChipDefines.ABSOLUTE_LIGHT_TYPE.parseContent(abs_light.s32State));
            }
        });

    }

    private void handVolume() {
        View customView = View.inflate(getContext(), R.layout.pup_volume, null);
        mPopupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setAnimationStyle(R.style.AnimToButtom);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mPopupWindow.showAtLocation(customView, Gravity.CENTER, 0, dm.heightPixels);

        ImageView iv_close = customView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        seekbar_input = customView.findViewById(R.id.seekbar_input);
        TextView txt_input_value = customView.findViewById(R.id.txt_input_value);

        if (mMyCamera.getChipVersion() == HiDeviceInfo.CHIP_VERSION_GOKE) {
            maxInputValue = 16;
            maxOutputValue = 13;
        }
        if (audio_attr != null) {
            seekbar_input.setProgress(audio_attr.u32InVol - 1);
            txt_input_value.setText(String.valueOf(audio_attr.u32InVol));
        }
        seekbar_input.setMax(maxInputValue - 1);
        seekbar_input.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                txt_input_value.setText(String.valueOf(seekBar.getProgress() + 1));
                sendToDevice();
            }
        });

        seekbar_output = customView.findViewById(R.id.seekbar_output);
        TextView txt_output_value = customView.findViewById(R.id.txt_output_value);
        if (audio_attr != null) {
            seekbar_output.setProgress(audio_attr.u32OutVol - 1);
            txt_output_value.setText(String.valueOf(audio_attr.u32OutVol));
        }
        seekbar_output.setMax(maxOutputValue - 1);
        seekbar_output.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                txt_output_value.setText(String.valueOf(seekBar.getProgress() + 1));
                sendToDevice();
            }
        });
    }

    private void sendToDevice() {
        if (audio_attr == null || seekbar_input == null || seekbar_output == null) {
            return;
        }
        int invol = seekbar_input.getProgress() + 1;
        int outvol = seekbar_output.getProgress() + 1;
        showjuHuaDialog();
        mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_AUDIO_ATTR, HiChipDefines.HI_P2P_S_AUDIO_ATTR.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, audio_attr.u32Enable, audio_attr.u32Stream, audio_attr.u32AudioType, 0, invol, outvol));
    }

    private void handPreset() {
        View customView = View.inflate(getContext(), R.layout.pup_preset, null);
        mPopupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setAnimationStyle(R.style.AnimToButtom);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mPopupWindow.showAtLocation(customView, Gravity.CENTER, 0, dm.heightPixels);
        ImageView iv_close = customView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        LiveViewModel.getInstance().handKeyBoard(getActivity(), customView, mMyCamera);

}

    private void handZoomFocus() {
        View customView = View.inflate(getContext(), R.layout.pup_zoom_focus, null);
        mPopupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setAnimationStyle(R.style.AnimToButtom);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        mPopupWindow.showAtLocation(customView, Gravity.CENTER, 0, dm.heightPixels);

        ImageView iv_close = customView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });

        TextView tv_zoomin = customView.findViewById(R.id.tv_zoomin);
        tv_zoomin.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_ZOOMIN, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_STOP, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                }
                return false;
            }
        });

        TextView tv_zoomout = customView.findViewById(R.id.tv_zoomout);
        tv_zoomout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_ZOOMOUT, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_STOP, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                }
                return false;
            }
        });

        TextView tv_focusin = customView.findViewById(R.id.tv_focusin);
        tv_focusin.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_FOCUSIN, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_STOP, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                }
                return false;
            }
        });

        TextView tv_focusout = customView.findViewById(R.id.tv_focusout);
        tv_focusout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_FOCUSOUT, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_CTRL, HiChipDefines.HI_P2P_S_PTZ_CTRL.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_CTRL_STOP, HiChipDefines.HI_P2P_PTZ_MODE_RUN, (short) MyLiveViewGLMonitor.PTZ_STEP, (short) 10));
                }
                return false;
            }
        });
    }

    private void handPupMirrorFlip() {
        View customView = View.inflate(getContext(), R.layout.pup_mirror_flip, null);
        mPopupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setAnimationStyle(R.style.AnimToButtom);
        mPopupWindow.setOutsideTouchable(false);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        ImageView iv_close = customView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.showAtLocation(customView, Gravity.CENTER, 0, dm.heightPixels);

        SwitchButton switch_btn_mirror = customView.findViewById(R.id.switch_btn_mirror);
        switch_btn_mirror.setChecked(display_param.u32Mirror == 1 ? true : false);
        switch_btn_mirror.setOnCheckedChangeListener(this);

        SwitchButton switch_btn_flip = customView.findViewById(R.id.switch_btn_flip);
        switch_btn_flip.setChecked(display_param.u32Flip == 1 ? true : false);
        switch_btn_flip.setOnCheckedChangeListener(this);

    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL:
                    handReceiveIoCtrl(msg);
                    break;
            }
        }
    };

    private void handReceiveIoCtrl(Message msg) {
        if (msg.arg2 == 0) {//成功
            handIoCtrlSuccess(msg);
        } else {
            hanIoCtrlFail(msg);
        }
    }

    private void hanIoCtrlFail(Message msg) {
        switch (msg.arg1) {
            case HiChipDefines.HI_P2P_GET_DISPLAY_PARAM:
                HiToast.showToast(getActivity(), getString(R.string.get_data_fail));
                break;
            case HiChipDefines.HI_P2P_SET_AUDIO_ATTR:
                HiToast.showToast(getActivity(), getString(R.string.application_fail));
                dismissjuHuaDialog();
                break;
        }
    }

    private void handIoCtrlSuccess(Message msg) {
        byte[] data = msg.getData().getByteArray(HiDataValue.EXTRAS_KEY_DATA);
        switch (msg.arg1) {
            case HiChipDefines.HI_P2P_GET_DISPLAY_PARAM:
                display_param = new HiChipDefines.HI_P2P_S_DISPLAY(data);
                if (mIsTXTJ) {
                    if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                    handPupImageAdjust();
                } else {
                    if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                    handPupMirrorFlip();
                }
                break;
            case HiChipDefines.HI_P2P_SET_DISPLAY_PARAM:
                dismissjuHuaDialog();
                break;
            case HiChipDefines.HI_P2P_GET_AUDIO_ATTR:
                audio_attr = new HiChipDefines.HI_P2P_S_AUDIO_ATTR(data);
                if(mPopupWindow!=null&&mPopupWindow.isShowing()){ return;}
                handVolume();
                break;
            case HiChipDefines.HI_P2P_SET_AUDIO_ATTR:
                dismissjuHuaDialog();
                break;
            case HiChipDefines.HI_P2P_GET_ABSOLUTE_LIGHT_TYPE:
                if (lightModel != 3) return;
                abs_light = new HiChipDefines.ABSOLUTE_LIGHT_TYPE(data);
                break;
            case HiChipDefines.HI_P2P_WHITE_LIGHT_GET_EXT:
                if (lightModel != 1) return;
                light_info_ext = new HiChipDefines.HI_P2P_WHITE_LIGHT_INFO_EXT(data);
                break;
            case HiChipDefines.HI_P2P_WHITE_LIGHT_GET:
                if (lightModel != 2) return;
                light_info = new HiChipDefines.HI_P2P_WHITE_LIGHT_INFO(data);
                break;
            case HiChipDefines.HI_P2P_GET_SMART_MOTO:
                trackParam = new HiChipDefines.HI_P2P_SMART_MOTO(data);
                boolean isSelect = trackParam.bSmartTrack == 0 ? false : true;
                if (lightModel != 0) {
                    ll_added_item_two.setSelected(isSelect);
                } else {
                    ll_added_item_one.setSelected(isSelect);
                }
                break;
        }
    }

    private void handPupImageAdjust() {
        View customView = View.inflate(getContext(), R.layout.pup_image_adjust, null);
        mPopupWindow = new PopupWindow(customView);
        ColorDrawable cd = new ColorDrawable(-000);
        mPopupWindow.setBackgroundDrawable(cd);
        mPopupWindow.setFocusable(false);
        mPopupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(mHeight);
        mPopupWindow.setAnimationStyle(R.style.AnimToButtom);
        mPopupWindow.setOutsideTouchable(false);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        ImageView iv_close = customView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
        mPopupWindow.showAtLocation(customView, Gravity.CENTER, 0, dm.heightPixels);

        SeekBar sb_brightness = customView.findViewById(R.id.sb_brightness);//亮度
        tv_brightness = customView.findViewById(R.id.tv_brightness);
        tv_brightness.setText(display_param.u32Brightness + "");
        sb_brightness.setMax(100);
        sb_brightness.setProgress(display_param.u32Brightness);
        sb_brightness.setOnSeekBarChangeListener(this);

        SeekBar sb_contrast = customView.findViewById(R.id.sb_contrast);//对比度
        tv_contrast = customView.findViewById(R.id.tv_contrast);
        tv_contrast.setText(display_param.u32Contrast + "");
        sb_contrast.setMax(100);
        sb_contrast.setProgress(display_param.u32Contrast);
        sb_contrast.setOnSeekBarChangeListener(this);

        SeekBar sb_saturation = customView.findViewById(R.id.sb_saturation);//饱和度
        tv_saturation = customView.findViewById(R.id.tv_saturation);
        tv_saturation.setText(display_param.u32Saturation + "");
        sb_saturation.setMax(255);
        sb_saturation.setProgress(display_param.u32Saturation);
        sb_saturation.setOnSeekBarChangeListener(this);
    }


    @Override
    public void onCheckedChanged(CompoundButton view, boolean isChecked) {
        switch (view.getId()) {
            case R.id.switch_btn_mirror:
                display_param.u32Mirror = isChecked ? 1 : 0;
                showjuHuaDialog();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_DISPLAY_PARAM, display_param.parseContent());
                break;
            case R.id.switch_btn_flip:
                display_param.u32Flip = isChecked ? 1 : 0;
                showjuHuaDialog();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_DISPLAY_PARAM, display_param.parseContent());
                break;
        }

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.sb_brightness:
                if (tv_brightness != null) {
                    tv_brightness.setText(seekBar.getProgress() + "");
                }
                break;
            case R.id.sb_contrast:
                if (tv_contrast != null) {
                    tv_contrast.setText(seekBar.getProgress() + "");
                }
                break;
            case R.id.sb_saturation:
                if (tv_saturation != null) {
                    tv_saturation.setText(seekBar.getProgress() + "");
                }
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        switch (seekBar.getId()) {
            case R.id.sb_brightness:
                display_param.u32Brightness = seekBar.getProgress();
                showjuHuaDialog();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_DISPLAY_PARAM, display_param.parseContent());
                break;
            case R.id.sb_contrast:
                display_param.u32Contrast = seekBar.getProgress();
                showjuHuaDialog();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_DISPLAY_PARAM, display_param.parseContent());
                break;
            case R.id.sb_saturation:
                display_param.u32Saturation = seekBar.getProgress();
                showjuHuaDialog();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_DISPLAY_PARAM, display_param.parseContent());
                break;
        }
    }
}
